package com.darkplanet.management.controller;


import com.darkplanet.management.domain.Project;
import com.darkplanet.management.domain.SecondMenu;
import com.darkplanet.management.service.ProjectService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/ProjectBaseAddServlet")
public class ProjectBaseAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("employee")!=null){
            List menus1 = (List) session.getAttribute("Menus");
            List<SecondMenu> menuList = (List<SecondMenu>) menus1.get(1);
            for (SecondMenu secondMenu : menuList) {
                if (secondMenu.getS_menu_id() == 1) {
                    ProjectService projectService = new ProjectService();
                    String project_name = request.getParameter("project_name");
                    String client_company = request.getParameter("client_company");
                    String customer_principal = request.getParameter("customer_principal");
                    String project_manager = request.getParameter("project_manager");
                    String developers_num_str = request.getParameter("developers_num");
                    int developers_num = Integer.parseInt(developers_num_str);
                    String start_time = request.getParameter("start_time");
                    String project_time = request.getParameter("project_time");
                    String estimated_cost_str = request.getParameter("estimated_cost");
                    double estimated_cost = Double.parseDouble(estimated_cost_str);
                    String level = request.getParameter("level");
                    String finish_time = request.getParameter("finish_time");
                    String remark = request.getParameter("remark");
                    projectService.addProject(project_name, client_company, customer_principal, project_manager, developers_num, start_time, project_time, estimated_cost, level, finish_time, remark);
                }
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
